from datetime import datetime

from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt

from tools.models.models import Department, UserInfo, TestUser


# Create your views here.
def index(request):
    return HttpResponse('欢迎使用！')

# 会按照APP注册顺序找HTML文件
def user_list(request):
    return render(request, 'tools/templates/testuser/user_list.html')

def user_add(request):
    return render(request, 'tools/templates/testuser/user_add.html')

def tpl(request):
    name = '张三'
    roles = ['管理员','CEO','张三']
    user_info = {'name':'李四','salary':'800', 'role':'CEO'}
    user_list = [
        {'name': 'zhangsan', 'salary': '800', 'role': 'CEO'},
        {'name': 'lisi', 'salary': '900', 'role': 'CFO'},
        {'name': 'wangwu', 'salary': '1000', 'role': '管理员'},
    ]
    return render(request, 'tools/templates/test/tpl.html',
                  {'n1': name, 'n2': roles, 'n3': user_info, 'n4': user_list} )


def news(req):
    # 定义一条新闻（字典或列表） 或 数据库 网络请求去联通新闻
    # 地址：
    # 第三方模块;requests (pip install requests)
    import requests
    res = requests.get('https://www.chinaunicom.com.cn/43/menu01/1/column01?pageNo=1&pageSize=10')
    # data_list = res.json(res)
    print(res)
    return render(req, 'tools/templates/test/news.html')


# def orm(req):
#     UserInfo.objects.create(name="zhangsan", password="123456", age=30, account='123',creatime=timezone.now(),depart_id=4)
#     UserInfo.objects.create(name="lisi", password="654321", age=20, account='456',creatime=timezone.now(),depart_id=5)
#     UserInfo.objects.create(name="wangmazi", password="415263", age=25, account='789',creatime=timezone.now(),depart_id=6)
#     return HttpResponse("添加成功！")

def orm2(req):
    # ########## 1.新建 ##########
    # Department.objects.create(title="销售部")
    # Department.objects.create(title="IT部")
    # Department.objects.create(title="制造部")
    # UserInfo.objects.create(name="zhangsan", password="123456", age=30, account='123',creatime=timezone.now(),depart_id=10,gender=1)
    # UserInfo.objects.create(name="lisi", password="654321", age=20, account='456',creatime=timezone.now(),depart_id=11,gender=2)
    # UserInfo.objects.create(name="wangmazi", password="415263", age=25, account='789',creatime=timezone.now(),depart_id=12,gender=1)
    # UserInfo.objects.create(name="二狗", password="415263")

    # # ########## 2.删除 ##########
    # UserInfo.objects.filter(id=5).delete()
    # Department.objects.all().delete()
    #
    # # ########## 3.查询 ##########
    # # 3.1 获取符合条件的所有数据
    # # data_list = [行对象,行对象,行对象,……]，QuerySet类型
    # data_list = UserInfo.objects.all()
    # print(data_list)
    # for obj in data_list:
    #     print(obj.id, obj.name, obj.password, obj.age)
    #
    # # data_list = [行对象,]
    # data_list = UserInfo.objects.filter(id=1)
    # print(data_list)
    #
    # # 获取第一条数据（对象）
    # row_obj = UserInfo.objects.filter(id=1).first()
    # print(row_obj.id, row_obj.name, row_obj.password, row_obj.age)
    #
    # # ########## 3.更新 ##########
    # UserInfo.objects.all().update(password='999999')
    # UserInfo.objects.filter(name='zhangsan').update(password='888888')

    return HttpResponse('成功！')


def info_list(request):
    # 1.获取数据库中所有用户信息
    # [对象，对象，对象]
    data_list = UserInfo.objects.all()
    print(data_list)

    # 2.渲染，返回给用户
    return render(request, "tools/templates/test/info_list.html", {"data_list": data_list})

@csrf_exempt  # 这个视图将跳过 CSRF 检查
def info_add(request):
    if request.method == "GET":
        return render(request, 'tools/templates/test/info_add.html')

    # 获取用户提交的数据
    testuser = request.POST.get("user")
    pwd = request.POST.get("pwd")
    age = request.POST.get("age")
    # account = request.POST.get("account")
    # creatime = request.POST.get("creatime")
    # creatime = datetime.now()
    # depart_id = request.POST.get("depart_id")
    # gender = request.POST.get("gender")

    # 添加到数据库
    TestUser.objects.create(name=testuser, password=pwd, age=age)

    # UserInfo.objects.create(name=testuser, password=pwd, age=age, account=account, creatime=creatime, depart_id=depart_id, gender=gender)

    # return HttpResponse("添加成功！")
    # 自动跳转
    return redirect("/tools/info/list/")

